package com.gr.blog.utils;

import java.io.IOException;
import java.io.InputStream;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import com.gr.blog.jira.dao.JiraDao;

public class ExcelUtils {

	private static final String EXCEL_XLS = "xls";
	private static final String EXCEL_XLSX = "xlsx";
	
	@Resource
	private JiraDao jiraDao;
	/**
	 * 判断Excel的版本,获取Workbook
	 * @author:巩斌鹏
	 * 2018年6月4日 下午2:38:58
	 * @param in
	 * @param file
	 * @return
	 * @throws IOException
	 * Workbook
	 */
	public Workbook getWorkbok(InputStream in, String fileName)
			throws IOException {
		Workbook wb = null;
		if (fileName.endsWith(EXCEL_XLS)) { // Excel 2003
			wb = new HSSFWorkbook(in);
		} else if (fileName.endsWith(EXCEL_XLSX)) { // Excel 2007/2010
			wb = new XSSFWorkbook(in);
		}
		return wb;
	}
	
	/**
	 * 判断文件是否是excel
	 * 
	 * @throws Exception
	 */
	public void checkExcelVaild(String fileName) throws Exception {
		if (!(fileName.endsWith(EXCEL_XLS) || fileName.endsWith(EXCEL_XLSX))) {
			throw new Exception("文件不是Excel");
		}
	}
	/**
	 * 判断每一个单元格里面值的属性，都转换成string并返回
	 * @author:巩斌鹏
	 * 2018年6月8日 下午8:05:29
	 * @param cell
	 * @return
	 * String
	 */
	public String vildCellType(Cell cell){
		Object obj = "";
		switch (cell.getCellType()) {
			case Cell.CELL_TYPE_STRING:
			    obj = cell.getStringCellValue();
			    break;
			case Cell.CELL_TYPE_NUMERIC:
			    obj = cell.getNumericCellValue();
			    break;
			case Cell.CELL_TYPE_FORMULA:
			    obj = cell.getCellFormula();
			    break;
			case Cell.CELL_TYPE_ERROR:
			    obj = cell.getErrorCellValue();
			    break;
			case Cell.CELL_TYPE_BOOLEAN:
			    obj = cell.getBooleanCellValue();
			    break;
			case Cell.CELL_TYPE_BLANK:
			    break;
		}
		return String.valueOf(obj).trim();

	}

}
